package com.msw.rest.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * TODO：一句话描述类的含义
 *
 * @Author hadoop
 * @Date 2016/4/10.
 */

public class JsonUtils {

    public static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class);

    public static final ObjectMapper MAPPER = new ObjectMapper();

    public static String formatObject(Object obj)
    {
        try {
            return MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            LOG.error("convert Object to json string error", e);
            return null;
        }
    }

    public static <T> T readToObject(String jsonStr, Class<T> targetClass)
    {
        try {
            return MAPPER.readValue(jsonStr, targetClass);
        } catch (IOException e) {
            LOG.error("convert jsonStr={} to Object error", jsonStr);
            return null;
        }
    }


}
